# ======================================================================
#
# Copyright (C) ADN
# ADN::SERVER::StartUP
# Description:
#  - Check et retablissement des processus au demarage
#  - Check and process restablish at startup 
#
# ======================================================================

package ADN::SERVER::StartUP;

use strict;
use ADN::LOG::Log;
use LWP::UserAgent;
use ADN::LANG::Lang;
use ADN::CONF::Path;
use ADN::CONF::Read;
use Proc::Background;
use ADN::SERVER::Constant qw(:constant :log);


# #######################
# LOG & LANG
# #######################
my $log = ADN::LOG::Log->new();
my %lang = ADN::LANG::Lang->new()->StartUP();;

# #######################
# THE CLEAN PROCESS
# #######################
sub cleanSchedule {
    my ( $exe , $func ) = folder();
    $func .= 'Clean.pl';
    Proc::Background->new( $exe, $func );
    return;
};

# #######################
# CATCH DIE SIG
# #######################
$SIG{__DIE__} = sub {
    my ( $tmp, $func, $exe );
    ( $exe , $func ) = folder();
    $log->Log( 'ERROR: DIE. CHECK LOG: error.log', HIGH, ALL );
    $tmp = readDIE();
    if ($tmp == 5) {
	   $log->Log( 'ERROR: DIE (5). CHECK LOG: error.log', HIGH, ALL );
	   unlink $func . 'die';
	   exit 1;
    }
    elsif ( $tmp != C_TRUE ) {
	   $tmp = $tmp + 1;
	   writeDIE( $tmp );
    }
    $func .= 'Server.pl';
    Proc::Background->new( $exe, $func, 'restart' );
};

# #######################
# WRITE DIE SIGNAL
# #######################
sub writeDIE {
    my $file = temp();
    $file .= 'die';
    if (open FILE, '>', $file) {
      print FILE $_[0];
      if (!close FILE) {}
    }
    undef $file;
    return;
}

# #######################
# READ DIE SIGNAL
# #######################
sub readDIE {
    my @tab;
    my $file = temp();
    $file .= 'die';
    if ( -e $file ) {
		  if (open FILE, '<', $file) {
		    $file = '';
				while ( <FILE> ) {
	    	  $file .= $_;
		    }
		    if (!close FILE) {}
				@tab = split /\n/smx, $file;
				undef $file;
	    	return $tab[0];
		  }
    }
    else {
		  writeDIE( '1' );
    }
		undef $file;
		undef @tab;
		return;    
}

# #######################
# LAUNCH THE ICON
# #######################
sub _icon {
    if ( $^O eq 'MSWin32' ) {
		  my ( $exe, $func ) = folder();
		  $func .= 'Icon.pl';
		  Proc::Background->new( $exe, $func );
    	undef $exe;
    	undef $func;
    }
	  return;
}

# #######################
# SERVER PATH FOLDER
# #######################
sub folder {
    my ( $conf, $func, $serviceBin, $path );
    my %folder;
    $conf = ADN::CONF::Read->new();
    $path = $conf->_send_path();
    %folder = $conf->service_server();
    $serviceBin = ADN::CONF::Path->new( $folder{bin} );
    $func = $path . $folder{service_data};
    undef $conf;
    undef %folder;
    undef $path;
    return ( $serviceBin, $func );
}

# #######################
# PID FILE
# #######################
sub pid {
   my $path = temp();
   if (open P, '>', "$path/adn.pid") {
    print P "${$}\n";
    if (!close P) {}
   }
   undef $path;
   return;
}

# #######################
# SERVER PATH TEMP / COMMON
# #######################
sub temp
{
  if ( $^O eq 'MSWin32' ) {
	       return $ENV{CommonProgramFiles} . '/ADN/';
  }
	elsif ( $^O eq 'linux' || $^O eq 'darwin' ) {
	       return '/tmp/ADN/';
	}
	return;
}

# #######################
# CHECK IF CONF FILE AND BIN
# #######################
sub _init {
    my ( $tmp, $conf );
    my %bin;
    $conf = ADN::CONF::Read->new();
    $tmp = $conf->_send_path();
    $tmp .= 'conf.xml';
    %bin = $conf->service_server();
    undef $conf;
    unless ( -e $tmp ) {
      $log->Log( $lang{mess1}, HIGH, ALL );
      exit C_TRUE;
    }
    if ( $^O eq 'MSWin32' && $bin{bin} ne 'perl.exe' ) {
      $log->Log( $lang{mess3}, HIGH, ALL );
      exit C_TRUE;
    }
    elsif (($^O eq 'linux' || $^O eq 'darwin') && $bin{bin} ne 'perl' ) {
      $log->Log( $lang{mess3}, HIGH, ALL );
      exit C_TRUE;
    }
    return;
}

# #######################
# READ PROCESS FILE
# #######################
sub _proc {
    my @proc;
    my $file = temp();
	  $file .= 'process';
	  if ( -e $file ) {
	   if (open List, '<', $file) {
	     while ( <List> ) {
	       chomp;
	       if ( $_ ne '' ) {
			     push @proc, $_;
	       }
	     }
	     if (!close List) {}
	    }
    }
    else {
		  unlink $file;
    }
    return @proc;
}

# #######################
# TEST THE PIPE
# #######################
sub startsock {
    my ( $file, $ua, $req, $res, $tmp );
    my %folder;
    $file = temp();
    $file .= 'die';
    %folder = ADN::CONF::Read->new()->server();
    $ua = LWP::UserAgent->new( timeout => '5' );
    $ua->agent( 'ADN API' );
    $req = HTTP::Request->new( POST => 'http://127.0.0.1:' . $folder{port} . '/' );
    $res = $ua->request( $req );
    if ( $res->is_success ) {
		  $log->Log( 'TEST ... OK', MED, TERM );
		  if ( -e $file ) {
	    	unlink $file;
		  }
	    undef $file;
		  return;
    }
    else {
		  $log->Log( "TEST ... NOK\n5s", 2, 4 );
		  if ( !defined ( $tmp = $_[0] )) {
	    	$tmp = 1;
		  }
		  if ( $tmp == 3 ) {
	    	exit;
		  }
		  $tmp = $tmp + 1;
		  startsock( $tmp );
		  undef $tmp;
    }
    return;
}

# #######################
# PRINT THE LOGO
# #######################
sub _start {
    $log->Log( $lang{mess2} . ' ...', LOW, ALL );
    _init();
    my @proc = _proc();
    if ( !defined $_[0] ) {
	     _icon();
    }
    sleep 1;
    if ( $^O eq 'MSWin32' ) {
	     system 'cls';
    }
    elsif ( $^O eq 'linux' ||  $^O eq 'darwin' ) {
	     system 'clear';
    }
    print <<"MESS";
\t\t     **      *******    ****     **
\t\t    ****    /**////**  /**/**   /**
\t\t   **//**   /**    /** /**//**  /**
\t\t  **  //**  /**    /** /** //** /**
\t\t ********** /**    /** /**  //**/**
\t\t/**//////** /**    **  /**   //****
\t\t/**     /** /*******   /**    //***
\t\t//      //  ///////    //      /// 
\t\t\t\t\t\t Copyright 2009 - ADN Logiciel
MESS
    return @proc;
}

# #######################
# DESTROY OBJECT
# #######################
sub DESTROY {
    my $self = shift;
    return;
}
1;
__END__
